// pages/other//choujiang/index.js
const Page = require('../../../utils/ald-stat.js').Page;
const app = getApp();
let interstitialAd = null;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    chouRule:true,
    page:1,
    haveMore:true,
    content:'',
    clickedsuc:true,
    trysuc:true,
    number:'',
    nocontent:false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const that = this;
    if (options.home == 1) {
      var object = {
        unionid: wx.getStorageSync('openid'),
        type: 26
      }
      var signs = app.sort_ASCII(object)
      wx.request({
        url: app.globalData.new_url + '/redpacket/Theme/activity_reward',
        data: {
          unionid: wx.getStorageSync('openid'),
          type: 26,
          sign: signs
        },
        method: 'POST',
        header: {
          'Cache-Control': 'max-age=60,public', //60秒
        },
        success: function (res) {
          console.log(res)
          if (res.data.code === 200) {
            that.setData({
              trysuc: false,
              clickedsuc: true,
              number: res.data.tc_reward,
            })
          }
        }
      })
    }
    if (wx.createInterstitialAd) {
      interstitialAd = wx.createInterstitialAd({
        adUnitId: 'adunit-8b780e723ac5fb97'
      })
      interstitialAd.onError((err) => {
        console.log('onError event emit', err)
      })
    } else {
      wx.showModal({
        title: '提示',
        content: '当前微信版本过低，无法使用该功能，请升级到最新微信版本后重试。'
      })
    }
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const that = this;
    that.data.page=1;
    that.data.haveMore = true;
    var obj = {
      unionid: wx.getStorageSync('openid'),
      page: 1
    }
    var sign = app.sort_ASCII(obj)
    wx.request({
      url: app.globalData.new_url + '/luck/luckydraw/index',
      data: {
        unionid: wx.getStorageSync('openid'),
        page: 1,
        sign: sign
      },
      method: 'POST',
      header: {
        'Cache-Control': 'max-age=60,public', //60秒
      },
      success: function (res) {
        console.log(res)
        if(res.data.code===200){
          that.setData({
            content: res.data.content
          })
        }else{
          that.setData({
            nocontent:true,
          })
        }
      }
    })
  },
  onReady: function () {
    if (interstitialAd) {
      interstitialAd.show().catch((err) => {
        console.error(err)
      })
    }
  },
  chouRule:function(){
    this.setData({
      chouRule: false,
    })
  },
  hideHandle:function(){
    const that=this;
    that.setData({
      chouRule:true,
    })
  },
  chourecord:function(){
    wx.navigateTo({
      url: '/pages/other/chourecord/index?type=1',
    })
  },
  choudetail:function(e){
    const id = e.target.dataset.id;
    wx.navigateTo({
      url: '/pages/other/choudetail/index?id='+id,
    })
  },
  zhongrecord: function() {
    wx.navigateTo({
      url: '/pages/other/chourecord/index?type=2',
    })
  },
  onReachBottom: function () {
    const that = this
    let page = that.data.page;
    var obj = {
      unionid: wx.getStorageSync('openid'),
      page: page
    }
    var sign = app.sort_ASCII(obj)
    if (that.data.haveMore) {
      // 请求下一页数据
      page++;
      that.data.page = page
      wx.request({
        url: app.globalData.new_url + '/luck/luckydraw/index',
        data: {
          unionid: wx.getStorageSync('openid'),
          page: page,
          sign: sign
        },
        method: 'POST',
        success: function (res) {
          console.log(res)
          if(res.data.code===200){
            that.data.content = that.data.content.concat(res.data.content);
            that.setData({
              content: that.data.content,
            })
            if (res.data.content.length > 10) {
              that.data.haveMore = true
            } else {
              that.data.haveMore = false
            }
          }
        }
      })
    } 
  },
  hidesuc:function(){
    const that=this;
    that.setData({
      clickedsuc: false,
      trysuc: true,
    })
  },
})